using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CE.Global;

public partial class controls_usrSalutation : System.Web.UI.UserControl
{
    protected string strSalution = "Select";
    protected string strValidationGroup = "";
    protected string strErrorMessage = "Choose salutation.";
    protected bool bEnabled = true;
    bool bReadOnly = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //DataTable dtSalutation = General.GetComboItem("TH.Salutation",  true);
            DataTable dtSalutation = General.GetComboItem(Resources.DBTable.SalutationList ,true);
            ddlSal.DataSource = dtSalutation;
            ddlSal.DataTextField = dtSalutation.Columns[0].ColumnName.ToString();
            ddlSal.DataValueField = dtSalutation.Columns[1].ColumnName.ToString();
            ddlSal.DataBind();
            ddlSal.Items.Insert(0, new ListItem("Select", "-1"));

            ddlSal.Attributes.Add("onblur", "ShowValidationEffect('" + ddlSal.ClientID + "','" + rfvSalutation.ClientID + "');");
        }
    }

    public void ChangeValidateEffect(bool bChange)
    {
        if (bChange)
        {
            ddlSal.ControlStyle.CssClass = "validationError";
        }
        else
        {
            ddlSal.ControlStyle.CssClass = "";
        }
    }


    public void ChangeValidationEffectServerSide(string strValidationGroup)
    {
        if (rfvSalutation.Enabled && rfvSalutation.ValidationGroup == strValidationGroup)
        {
            rfvSalutation.Validate();
            if (rfvSalutation.IsValid == false)
            {
                ddlSal.ControlStyle.CssClass = "validationError";
            }
            else
            {
                ddlSal.ControlStyle.CssClass = "";
            }
        }
        else
        {
            ddlSal.ControlStyle.CssClass = "";
        }
    }
    
    //End no good performance

    //protected void ServerValidation(object source, ServerValidateEventArgs args)
    //{
    //    try
    //    {
    //        // Test whether the value is -1
    //        if (args.Value != "-1")
    //        {
    //            args.IsValid = true;
    //        }
    //        else
    //        {
    //            args.IsValid = false;
    //        }
    //    }

    //    catch (Exception ex)
    //    {
    //        args.IsValid = false;
    //    }
    //}

    public bool IsReadOnly
    {
        get { return bReadOnly; }
        set
        {
            bReadOnly = value;
            //SetReadOnly();
        }
    }

    public bool IsEnabled
    {
        get { return bEnabled; }
        set
        {
            bEnabled = value;
            SetEnabled();
        }
    }

    public void IsEnableValidation(bool bValue)
    {
        rfvSalutation.Enabled = bValue;
    }

    public string Saluation
    {
        get { return strSalution; }
        set { strSalution = value; }
    }

    public string ValidationGroup
    {
        get { return strValidationGroup; }
        set { strValidationGroup = value; }
    }

    public string ErrorMessage
    {
        get { return strErrorMessage; }
        set { strErrorMessage = value; }
    }

    private void SetEnabled()
    {
        //ddlSal.SelectedIndex = 0;
        ddlSal.Enabled = bEnabled;
        rfvSalutation.Enabled = bEnabled;
    }

    protected override void OnPreRender(EventArgs e)
    {
        if (strSalution != string.Empty)
        {
            if (strSalution != "Select")
                ddlSal.Text = strSalution;
        }

        if (strValidationGroup != null)
        {
            if (strValidationGroup != string.Empty)
                rfvSalutation.ValidationGroup = strValidationGroup;
        }

        if (bReadOnly)
        {
            SetReadOnly();
        }

        if (strErrorMessage != string.Empty)
        {
            //rfvSalutation.ErrorMessage = strErrorMessage;
        }
        
    }

    public string GetSalutation()
    {
        strSalution = ddlSal.SelectedValue;
        return strSalution;
    }

    private void SetReadOnly()
    {
        if (bReadOnly)
        {
            ddlSal.Text = strSalution;

            string orgSalText = ddlSal.SelectedItem.Text;
            string orgSalValue = ddlSal.SelectedItem.Value;

            ddlSal.Items.Clear();

            ddlSal.Items.Add(new ListItem(orgSalText, orgSalValue));
        }
    }

    public bool IsValidRequirement()
    {
        rfvSalutation.Validate();
        return rfvSalutation.IsValid;
    }
}
